﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 循环计算
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
      
        string[][] w1 = new string[4][];
        private void button1_Click(object sender, EventArgs e)
        {  
            w1[0]=new string[4];
            w1[2] = new string[4];
            w1[3] = new string[4];
            w1[1]=new string[4];
            string s1 = "=A1+B1*2+C1*2+A2";
            string A1= "=B1+C1+A2";
            string A2 = "=B1+C1";
            string B1 = "3";
            string C1 = "4";
            w1[2][1] = textBox1.Text;
            w1[3][1] = textBox2.Text;
            w1[1][1] = A1;
            w1[1][2] = A2;



            label3.Text = chuli1(A1); label4.Text = chuli2(chuli1(A1));
            label2.Text = chuli2(chuli1(s1));label1.Text = chuli1(s1);
        }
        
        string zhuan;
        //处理原始字符串 
        string chuli1(string s)
        {string s2=s;
        string s3 = s;
       
            s2 = s2.Replace("+", "|+|");
            s2 = s2.Replace("=", "");
            s2 = s2.Replace("-", "|-|");
            s2 = s2.Replace("*", "|*|");
            s2 = s2.Replace("/", "|/|");
            s2 = s2.Replace("(", "(|");
            s2 = s2.Replace(")", "|)");
            string[] s1 = s2.Split('|');
            int n = s1.Length;
            for (int i = 0; i < n;i++ )
            {
                if((char)s1[i][0]>=65&&(char)s1[i][0]<=90)
                {
                    string z = zhuanhuan(s1[i]);
                    s3 = s3.Replace(s1[i], z);
                }
            }
                return s3;
        }
        // 将含有字母的单元转换为(横纵坐标)的内容
        string zhuanhuan(string s1)
        {
           
            int n = 0;
            string ss = "";
            if ((char)s1[1] >= 65 && (char)s1[1] <= 90)
            {
                char a = s1[0];
                char b = s1[1];

                n = (Convert.ToInt32(a) - 64) * 26 + (Convert.ToInt32(b) - 64);
                for (int i = 2; i < s1.Length; i++)
                {
                    ss += s1[i];
                }
                zhuan = w1[n][int.Parse(ss)].ToString();
            }
            else
            {
                char a = s1[0];
                n = Convert.ToInt32(a) - 64;


                for (int i = 1; i < s1.Length; i++)
                {
                    ss += s1[i];
                }
                zhuan = w1[n][int.Parse(ss)];
                if(zhuan[0]=='=')
                {
                   
                    zhuan=chuli1(zhuan);
                    zhuan=chuli2(zhuan);
                    if(int.Parse(zhuan)<0)
                    {
                        zhuan = "0" + zhuan;
                    }

                }
            }
            return zhuan;
        }

        //对括号的处理
        string chuli2(string s)
        {
            string str = s;
            string last;

            int kuohaonum = 0;
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] == '(')
                {
                    kuohaonum++;
                }
            }

            while (kuohaonum != 0)
            {
                kuohaonum--;
                int star = str.IndexOf('(') + 1;
                int sle = str.IndexOf(')') - star;
                string s1 = str.Substring(star, sle);

                int s2 = jisuan(s1);
                string s5;
                if (s2 < 0)
                {
                    s5 = "0" + s2.ToString();
                }
                else
                    s5 = s2.ToString();
                int star1 = star - 1;
                int sle1 = sle + 2;
                string s3 = str.Substring(star1, sle1);
                str = str.Replace(s3, s5);
            }
            last = jisuan(str).ToString();
            return last;


        }
        //对不带括号的字符串进行计算
        int jisuan(string s)
        {  //存数字
            Stack<string> shu = new Stack<string>();
            //存符号
            Stack<string> fu = new Stack<string>();

            int n = s.Length;
            //label1.Text = n.ToString();
            s = s.Replace("+", "|+|");
            s = s.Replace("=", "");
            s = s.Replace("-", "|-|");
            s = s.Replace("*", "|*|");
            s = s.Replace("/", "|/|");
            s = s.Replace("(", "(|");
            s = s.Replace(")", "|)");
            //  label1.Text = s;
            string[] s1 = s.Split('|');
            label1.Text = "";

            int jieguo = 0;
            //对符号中的乘除先计算
            for (int i = 0; i < s1.Length; i++)
            {
                if (s1[i] == "+" || s1[i] == "-" || s1[i] == "*" || s1[i] == "/")
                {
                    fu.Push(s1[i]);
                }
                else
                {
                    if (fu.Count == 0)
                    {
                        shu.Push(s1[i]);
                        continue;
                    }
                    else
                    {
                        if (fu.Peek() == "+" || fu.Peek() == "-")
                        {
                            shu.Push(s1[i]);
                        }
                        else
                        {
                            int q1 = int.Parse(s1[i]);
                            int q2 = int.Parse(shu.Peek());
                            shu.Pop();
                            string q3 = fu.Peek();
                            fu.Pop();
                            int q4 = 0;
                            if (q3 == "*")
                            {
                                q4 = q1 * q2;
                            }
                            if (q3 == "/")
                            {
                                q4 = q2 / q1;
                            }
                            shu.Push(q4.ToString());
                            jieguo = q4;
                        }
                    }
                }
            }
            //对栈的顺序颠倒然后按照从左到右计算
            List<string> shu1 = new List<string>();
            List<string> fu1 = new List<string>();
            while (shu.Count != 0)
            {
                string r = shu.Peek();
                shu.Pop();
                shu1.Add(r);

            }
            while (fu.Count != 0)
            {
                string r = fu.Peek();
                fu.Pop();
                fu1.Add(r);
            }
            while (shu1.Count != 0)
            {
                string oo = shu1[0];
                shu.Push(oo);
                shu1.Remove(oo);
            }
            while (fu1.Count != 0)
            {
                string oo = fu1[0];
                fu.Push(oo);
                fu1.Remove(oo);

            }
            while (fu.Count != 0)
            {
                string w1 = fu.Peek();
                fu.Pop();
                int qian = int.Parse(shu.Peek());
                shu.Pop();
                int hou = int.Parse(shu.Peek());
                shu.Pop();
                if (w1 == "+")
                {
                    jieguo = qian + hou;
                    shu.Push(jieguo.ToString());
                }
                if (w1 == "-")
                {
                    jieguo = qian - hou;
                    shu.Push(jieguo.ToString());
                }
            }
            return jieguo;
        }
    }
}
